package com.jadcon.bedbreakfast.domain;

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( { "/spring-security.xml", "/spring-service.xml" })
public class CustomerServiceTestIntegration {

	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private CustomerRepository customerRepository;

	@SuppressWarnings("unused")
	@Test
	public void getCustomerList() {
		List<Customer> customerList = customerRepository.getCustomerList();
	}
	
	
	@Test
	public void saveNewCustomer() {
		Customer customer = new Customer();
		String username = "john";
		customer.setUsername(username);
		customer.setPassword("password");
		customer.setName("John Doe");
		customer.setName2("Your Company");
		customer.setAddress1("1234 St.");
		customer.setAddress2("empty");
		customer.setCity("Town");
		customer.setState("PA");
		customer.setZip(23415);
		customer.setEmail("Johndoe@gmail.com");

		List<PhoneNumber> phoneNumberList = new ArrayList<PhoneNumber>();
		customer.setPhoneNumberList(phoneNumberList);
		PhoneNumber homePhone = new PhoneNumber();
		homePhone.setAreaCode(610);
		homePhone.setExchange(327);
		homePhone.setLineNumber(3973);
		homePhone.setExtension(5000);
		homePhone.setPhoneType(PhoneType.HOME);
		PhoneNumber cellPhone = new PhoneNumber();
		cellPhone.setAreaCode(570);
		cellPhone.setExchange(849);
		cellPhone.setLineNumber(0055);
		cellPhone.setPhoneType(PhoneType.CELL);
		phoneNumberList.add(homePhone);
		phoneNumberList.add(cellPhone);

		customerService.save(customer);
				
		Customer savedCustomer = customerService.findByUsername(username);
		customer.setId(savedCustomer.getId());
	
		List<PhoneNumber> phoneList = savedCustomer.getPhoneNumberList();
		for (PhoneNumber phoneNumber : phoneList){
			phoneNumber.setId(0);
		}
	}

	@Test
	public void findByName() {
		String name = "John Doe";
		List<Customer> customerList = customerService.findByName(name);
		Customer customer = customerList.get(0);
		System.out.println("findByName() - Customer name is: " + customer.getName());
		Assert.assertTrue("customerList is empty", !customerList.isEmpty());
	}

	@Test
	public void findById() {
		String name = "John Doe";
		List<Customer> customerList = customerService.findByName(name);
		Customer customer = customerList.get(0);
		int id = customer.getId();
		List<Customer> customerList2 = customerService.findByID(id);
		Customer customerId = customerList2.get(0);
		System.out.println("getCustomerById() - Customer name is: " + customerId.getName());
		Assert.assertEquals("customerList.size wrong", 1, customerList.size());
	}
	
	@SuppressWarnings("unused")
	@Test
	public void findByUsername(){
		Customer customer = customerService.findByUsername("john");
	}
	
	@SuppressWarnings("unused")
	@Test
	public void getStateList(){
		List<State> stateList = customerService.getStateList();

	}
	
	@SuppressWarnings("unused")
	@Test
	public void usernameAvailable(){
		Long time = System.currentTimeMillis();
		String username = new String ("username" + time);
		Customer customer = new Customer();
		customer.setUsername(username);
		Boolean available = customerService.usernameAvailable(customer);
	}
	
	@Test
	public void delete(){
		Customer customer = customerService.findByUsername("john");
		int id = customer.getId();
		customerService.delete(id);
	}

}
